<?php
namespace Api\Controller;
use Think\Controller;
use Think\Vender;
class RefundController extends BaseController{
	const REFUNDURL="https://api.mch.weixin.qq.com/secapi/pay/refund";
	private $params;
	public function __construct(){
		parent::__construct(); //继承base类 同时继承base类里面的构造方法
		$order=I('post.order');
		$where["order_no|transaction_id"] = array("like","%".$order."%");
		$data=M('ad_order')->where($where)->find();
		$this->params=[
			'appid'   =>  self::APPID, //APPID  
		    'mch_id'  =>  self::MCH_ID, //商户号
		    'nonce_str'=> md5(time()), //随机串
		    'sign'  => 'md5',          //签名方式
		    //'transaction_id'=> $data['transaction_id'],//微信支付订单号 与商户订单号二选一
		    'out_trade_no'	=> $data['order_no'], //商户订单号 和微信支付订单号二选一
		    'out_refund_no' => '1001'.rand(1000,10000),//退单号 每次退款号码不能一致。
		    'total_fee'     => $data['price'],    //订单金额
		    'refund_fee'    => 1    //退款金额
		];
		
	}
	public function orderRefund(){
		//生成签名
		$singParams=$this->setSign($this->params);
		//将数组转化成xml数据
		$xmlData = $this->arrayToXml($singParams);
		//发送请求 
		$result=$this->postData(self::REFUNDURL,$xmlData);
		//xml数据转换成数组
		$data=$this->xmlToArray($result);	
		if(($data['return_code']=='SUCCESS' && $data['return_msg']=='OK') && $data['err_code']!='ERROR'){
			$datas['appid']=$data['appid'];
			$datas['mch_id']=$data['mch_id'];
			$datas['out_trade_no']=$data['out_trade_no'];
			$datas['transaction_id']=$data['transaction_id'];
			$datas['refund_id']=$data['refund_id'];
			$datas['refund_fee']=$data['refund_fee'];
			$datas['time']=time();
			$re=M('refund')->add($datas);
			if($re){
				$res['code']=200;
				$res['msg']='退款成功保存数据成功！';
				$this->ajaxReturn($res);
			}else{
				$res['code']=400;
				$res['msg']='退款成功保存退款信息失败！';
				$this->ajaxReturn($res);
			}
		}else{
			$res['code']=404;
			$res['msg']='该笔订单金额已全部退款完毕！';
			$this->ajaxReturn($res);
		}
	}

	function postData($url,$xml,$second=30){
	    //初始化curl          
        $ch = curl_init();  
        //设置超时  
        curl_setopt($ch, CURLOP_TIMEOUT, $second);  
        curl_setopt($ch,CURLOPT_URL, $url);  
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);  //不检测服务器安装证书是否正确
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);  
        //设置header  
        curl_setopt($ch, CURLOPT_HEADER, FALSE);  //设置为false
        //要求结果为字符串且输出到屏幕上  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
        //以下是证书相关代码
		  curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
		 curl_setopt($ch,CURLOPT_SSLCERT,'./Public/cert/apiclient_cert.pem');
		 curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
		  curl_setopt($ch,CURLOPT_SSLKEY,'./Public/cert/apiclient_key.pem');
        //post提交方式  
        curl_setopt($ch, CURLOPT_POST, TRUE);  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);  
        //运行curl  
        $data = curl_exec($ch);  
        curl_close($ch);  
        //返回结果  
        if($data){  
            curl_close($ch);  
            return $data;  
        }  
        else{   
            $error = curl_error($ch);  
            echo "curl出错，错误码:".var_dump(curl_error($ch))."<br>";     
            curl_close($ch);  
            return false;  
        }  
	}

	public function ceshi(){
		$a=0;
		if($a!=false){
			echo 'zhen';
		}else{
			echo 'jia';
		}
	}
}